package org.groupg.practice.guava;

import com.google.common.math.DoubleMath;
import com.google.common.math.IntMath;
import com.google.common.math.LongMath;
import com.google.common.math.BigIntegerMath;

import java.math.BigInteger;
import java.math.RoundingMode;

public class GuavaMathExample {

    public static void main(String[] args) {
        // IntMath 示例：计算阶乘
        int factorial = IntMath.factorial(5);
        System.out.println("IntMath 阶乘: 5! = " + factorial);

        // LongMath 示例：计算幂
        long power = LongMath.pow(2, 10);
        System.out.println("LongMath 幂: 2^10 = " + power);

        // DoubleMath 示例：计算模糊相等的浮点数
        boolean fuzzyEquals = DoubleMath.fuzzyEquals(1.0, 1.0000001, 0.00001);
        System.out.println("DoubleMath 模糊相等: 1.0 ≈ 1.0000001 = " + fuzzyEquals);

        // BigIntegerMath 示例：计算平方根并取整
        BigIntegerMath.sqrt(BigInteger.TEN.pow(99), RoundingMode.HALF_EVEN);
        System.out.println("BigIntegerMath 平方根: sqrt(10^99) rounded to nearest even = " +
                           BigIntegerMath.sqrt(BigInteger.TEN.pow(99), RoundingMode.HALF_EVEN));

        // LongMath 示例：带溢出检查的运算
        try {
            long overflowResult = LongMath.checkedAdd(Long.MAX_VALUE, 1);
        } catch (ArithmeticException e) {
            System.out.println("LongMath 溢出检查: Long.MAX_VALUE + 1 导致溢出");
        }

        // LongMath 示例：除法并取整
        long quotient = LongMath.divide(10, 3, RoundingMode.CEILING);
        System.out.println("LongMath 除法并取整: 10 / 3 = " + quotient);
    }
}